package com.ruoyi.flow.flow.task;

import com.ruoyi.flow.activiti.security.SecurityUtils;
import com.ruoyi.flow.activiti.service.api.ModelService;
import com.ruoyi.flow.auth.security.FlowUserDetailService;
import com.ruoyi.flow.common.Page;
import com.ruoyi.flow.core.response.Response;
import com.ruoyi.flow.flow.workflow.FlowService;
import com.ruoyi.flow.flow.workflow.FlowTask;
import com.ruoyi.flow.flow.workflow.FlowUtils;
import org.activiti.engine.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Optional;

/**
 * @Author: wtian
 * @Description:
 * @Date: Created in 10:49 2018/8/16
 * @Version: 1.0
 */
@Controller
@RequestMapping("/flow/tasks")
public class TaskController {
    private static final Logger LOGGER = LoggerFactory.getLogger(TaskController.class);
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private IdentityService identityService;
    @Autowired
    private FormService formService;
    @Autowired
    private ModelService modelService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private FlowUserDetailService userDetailService;
    @Autowired
    private HistoryService historyService;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView toListPage(@RequestParam Optional<String> action) {
        ModelAndView mav = new ModelAndView("admin/task/tasks");
        String actionName = "todo";
        if (action.isPresent()) {
            actionName = action.get();
        }
        mav.addObject("action", actionName);
        return mav;
    }

    /**
     * 获取我的待办列表
     *
     * @param page 分页信息
     * @return
     */
    @RequestMapping(value = "/todo", method = RequestMethod.GET)
    @ResponseBody
    public Page<FlowTask> toDoTasks(Page<FlowTask> page) {
        String userId = SecurityUtils.getCurrentUserId();
        return FlowService.taskService().getToDoTasks(userId, page);
    }

    /**
     * 我的已办
     *
     * @param page
     * @return
     */
    @RequestMapping(value = "/done", method = RequestMethod.GET)
    @ResponseBody
    public Page<FlowTask> doneTasks(Page<FlowTask> page) {
        String userId = SecurityUtils.getCurrentUserId();
        page = FlowService.taskService().getDoneTasks(userId, page);
        return page;
    }

    /**
     * 执行任务办理
     *
     * @param taskId    任务ID
     * @param assignees 下一步办理人
     * @param comment   备注信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/{taskId}", method = RequestMethod.POST)
    @ResponseBody
    public Response<FlowTask> doTask(@PathVariable String taskId, @RequestParam Optional<String> assignees, @RequestParam Optional<String> comment, HttpServletRequest request) {
        Map<String, Object> variables = FlowUtils.getProcessVariableFromRequest(request);
        String _assignees = null;
        if (assignees.isPresent()) {
            _assignees = assignees.get();
        }
        FlowTask flowTask = null;
        String _comment = comment.isPresent() ? comment.get() : null;
        try {
            flowTask = FlowService.taskService().doTask(taskId, _assignees, _comment, SecurityUtils.getCurrentUserId(), variables);
            return Response.<FlowTask>success().setData(flowTask);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Response.error();
    }


}
